package com.biz.primus.management.commodity.init;

import com.biz.primus.account.service.AuthInitializeService;
import com.biz.primus.base.enums.CommonStatus;
import com.biz.primus.model.sso.auth.vo.GrantedAuthority;
import com.biz.primus.model.sso.auth.vo.MenuGroupVO;
import com.biz.primus.model.sso.auth.vo.MenuItemVO;
import com.biz.primus.model.sso.auth.vo.ResourceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Created by defei on 9/19/17.
 */
@Service
public class InitManager {

    private final AuthInitializeService authInitializeService;

    @Autowired
    public InitManager(AuthInitializeService authInitializeService) {
        this.authInitializeService = authInitializeService;
    }

    @PostConstruct
    public void setup() {

        List<MenuGroupVO> menuGroups = newArrayList();
        MenuGroupVO menuGroupVO = new MenuGroupVO("MANAGEMENT_COMMODITY", "商品管理", "商品管理", "fa fa-cogs", CommonStatus.ENABLE, 999);
        List<MenuItemVO> menuItems = newArrayList();
        MenuItemVO menuManageMenuItemVO = new MenuItemVO("MANAGEMENT_COMMODITY_CATEGORY", "分类管理", "显示分类列表", "fa fa-list", "management/category/list", CommonStatus.ENABLE, 1);
        menuManageMenuItemVO.setGrantedAuthority(new GrantedAuthority("ROLE_COMMODITY;OPT_CATEGORY_LIST"));
        List<ResourceVO> resources = newArrayList();
        resources.add(new ResourceVO("MANAGEMENT_COMMODITY_CATEGORY_ADD", "添加", "添加分类", CommonStatus.ENABLE, new GrantedAuthority("OPT_CATEGORY_UPDATE"), 1));
        resources.add(new ResourceVO("MANAGEMENT_COMMODITY_CATEGORY_DETAIL", "详情", "分类详情", CommonStatus.ENABLE, new GrantedAuthority("OPT_CATEGORY_DETAIL"), 2));
        menuManageMenuItemVO.setResources(resources);
        menuItems.add(menuManageMenuItemVO);

        MenuItemVO brandMenuItemVO = new MenuItemVO("MANAGEMENT_COMMODITY_BRAND", "品牌管理", "显示品牌列表", "fa fa-list", "management/brand/list", CommonStatus.ENABLE, 2);
        brandMenuItemVO.setGrantedAuthority(new GrantedAuthority("ROLE_COMMODITY;OPT_BRAND_LIST"));
        List<ResourceVO> brandResources = newArrayList();
        brandResources.add(new ResourceVO("MANAGEMENT_COMMODITY_BRAND_ADD","添加","添加品牌",CommonStatus.ENABLE,new GrantedAuthority("OPT_BRAND_UPDATE"),1));
        brandResources.add(new ResourceVO("MANAGEMENT_COMMODITY_BRAND_DETAIL","详情","品牌详情",CommonStatus.ENABLE,new GrantedAuthority("OPT_BRAND_DETAIL"),1));
        brandMenuItemVO.setResources(brandResources);
        menuItems.add(brandMenuItemVO);

        MenuItemVO saletagMenuItemVO = new MenuItemVO("MANAGEMENT_COMMODITY_SALETAG", "标签管理", "显示标签列表", "fa fa-list", "management/saletag/list", CommonStatus.ENABLE, 2);
        saletagMenuItemVO.setGrantedAuthority(new GrantedAuthority("ROLE_COMMODITY;OPT_SALETAG_LIST"));
        List<ResourceVO> saletagResources = newArrayList();
        saletagResources.add(new ResourceVO("MANAGEMENT_COMMODITY_SALETAG_ADD","更新","添加标签",CommonStatus.ENABLE,new GrantedAuthority("OPT_SALETAG_UPDATE"),1));
        saletagResources.add(new ResourceVO("MANAGEMENT_COMMODITY_SALETAG_DETAIL","详情","标签详情",CommonStatus.ENABLE,new GrantedAuthority("OPT_SALETAG_DETAIL"),1));
        saletagResources.add(new ResourceVO("MANAGEMENT_COMMODITY_SALETAG_CREATE","创建","创建标签",CommonStatus.ENABLE,new GrantedAuthority("OPT_SALETAG_CREATE"),1));
        saletagMenuItemVO.setResources(saletagResources);
        menuItems.add(saletagMenuItemVO);

        MenuItemVO extendMenuItemVO = new MenuItemVO("MANAGEMENT_COMMODITY_EXTEND", "扩展属性管理", "显示扩展属性列表", "fa fa-list", "management/extend/list", CommonStatus.ENABLE, 2);
        extendMenuItemVO.setGrantedAuthority(new GrantedAuthority("ROLE_COMMODITY;OPT_EXTEND_LIST"));
        List<ResourceVO> extendResources = newArrayList();
        extendResources.add(new ResourceVO("MANAGEMENT_COMMODITY_EXTEND_ADD","更新","添加扩展属性",CommonStatus.ENABLE,new GrantedAuthority("OPT_EXTEND_UPDATE"),1));
        extendResources.add(new ResourceVO("MANAGEMENT_COMMODITY_EXTEND_DETAIL","详情","扩展属性详情",CommonStatus.ENABLE,new GrantedAuthority("OPT_EXTEND_DETAIL"),1));
        extendResources.add(new ResourceVO("MANAGEMENT_COMMODITY_EXTEND_CREATE","创建","创建扩展属性",CommonStatus.ENABLE,new GrantedAuthority("OPT_EXTEND_CREATE"),1));
        extendMenuItemVO.setResources(extendResources);
        menuItems.add(extendMenuItemVO);

        menuGroupVO.setMenuItems(menuItems);
        menuGroups.add(menuGroupVO);
        authInitializeService.registerMenu(menuGroups);
    }

}
